package console

import (
	"fmt"
	"leaf/chanrpc"
)

type userCommand struct {
	name   string
	help   string
	server *chanrpc.Server
}

func (command *userCommand) Name() string {
	return command.name
}

func (command *userCommand) Help() string {
	return command.help
}

func (command *userCommand) Run(_args []string) (result string) {
	args := make([]interface{}, len(_args))
	for i, v := range _args {
		args[i] = v
	}

	info, err := command.server.Call1(command.name, args...)
	if err != nil {
		return err.Error()
	}

	type iString interface {
		String() string
	}

	switch info.(type) {
	case nil:
	case string:
		result = info.(string)
	case iString:
		result = info.(iString).String()
	default:
		result = fmt.Sprintf("%v", info)
	}

	return
}
